﻿Imports System.IO

Namespace Diagnostics.Profiler
    Public Class ProfilerViewer

        Private Sub ProfilerViewer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
        End Sub

        Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
            Dim totalTime As TimeSpan = TimeSpan.Zero

            Dim result As DialogResult = OpenFileDialog1.ShowDialog()
            If result <> Windows.Forms.DialogResult.OK Then
                Return
            End If

            ProfilerDataSet.Clear()
            Using fs As Stream = OpenFileDialog1.OpenFile(), reader As New StreamReader(fs)
                Do
                    Dim line As String = reader.ReadLine()
                    If line Is Nothing Then
                        Exit Do
                    End If
                    Try
                        Dim values As String() = line.Split(Char.Parse(vbTab))
                        Dim methodName As String = values(0)
                        Dim count As Integer = Integer.Parse(values(1))
                        Dim time As TimeSpan = TimeSpan.Parse(values(2))
                        ProfilerDataSet.ProfilerInfo.AddProfilerInfoRow( _
                            methodName, count, time, 0)
                        totalTime += time
                    Catch ex As IndexOutOfRangeException
                        Continue Do
                    Catch ex As FormatException
                        Continue Do
                    End Try
                Loop
            End Using

            For Each row As ProfilerDataSet.ProfilerInfoRow In ProfilerDataSet.ProfilerInfo.Rows
                row.Rate = row.Time.Ticks / totalTime.Ticks
            Next

            DataGridView1.DataSource = ProfilerDataSet.ProfilerInfo
        End Sub

        Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
            Clipboard.SetDataObject(DataGridView1.GetClipboardContent())
        End Sub
    End Class
End Namespace
